# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: get_geoip.py 
@time: 19-4-10 上午11:31
"""

import geoip2.database
reader = geoip2.database.Reader('/usr/share/GeoIP/GeoLite2-City.mmdb')
data = reader.city('111.161.38.122')
print(data.city.name)
print(data.country.name)
print(data.location.latitude)
print(data.location.longitude)

import pygeoip

pgi = pygeoip.GeoIP('/usr/share/GeoIP/GeoIPCity.dat')

def printRecord(tgt):
    rec = pgi.record_by_addr(tgt)
    city = rec['city']
    region = rec['region_code']
    country = rec['country_name']
    long = rec['longitude']
    lat = rec['latitude']
    return '[*] 主机: {} Geo-located.\n[+] {}, {}, {},\n[+] 经度: {}, 维度: {} '.format(tgt,str(city),str(region),str(country), str(long),str(lat))

tgt='111.161.38.122'
print(printRecord(tgt))
print(printRecord('103.231.145.122'))